import distutils.spawn
import os
import re
import shlex
import subprocess
import sys

from setuptools import setup, find_packages


def get_version():
    filename = "returnlabels/__init__.py"
    with open(filename, 'r', encoding='utf8') as f:
        match = re.search(
            r"""^__version__ = ['"]([^'"]*)['"]""", f.read(), re.M
        )
    if not match:
        raise RuntimeError("{} doesn't contain __version__".format(filename))
    version = match.groups()[0]
    return version


def get_install_requires():
    install_requires = [
        "imgviz>=0.11",
        "termcolor",
        "matplotlib<3.3",
        "opencv_python<4.3.0",
        "opencv_contrib_python<4.3.0",
        "numpy",
        "pandas",
        "Pillow>=2.8",
        "PyYAML",
        "qtpy",
        "tqdm",
        "requests",
        "pyqt5<=5.15.10",
        "openpyxl",
    ]
    if os.path.exists("requirements.txt"):
        with open("requirements.txt", "r", encoding="utf8") as fp:
            data = fp.readlines()
        for row in data:
            if row.strip("\n") not in install_requires:
                install_requires.append(row.strip("\n"))

    if os.name == "nt":  # Windows
        install_requires.append("colorama")

    return install_requires


def get_long_description():
    with open("README.md", 'r', encoding="utf8") as f:
        long_description = f.read()
    try:
        import github2pypi

        return github2pypi.replace_url(
            slug="coredata-ai/returnlabels", content=long_description
        )
    except Exception:
        return long_description


def main():
    version = get_version()

    setup(
        name="ReturnLabels",
        version=version,
        packages=find_packages(exclude=["github2pypi"]),
        description="Return labels.",
        long_description=get_long_description(),
        long_description_content_type="text/markdown",
        author="kelamini",
        author_email="kelamini_0216@163.com",
        url="https://gitlab.com/coredata-ai/returnlabels.git",
        install_requires=get_install_requires(),
        license="Apache License Version 2.0",
        keywords="Return labels",
        classifiers=[
            "Development Status :: 3 - Production/Stable",
            "Intended Audience :: Developers",
            "Natural Language :: English",
            "Programming Language :: Python",
            "Programming Language :: Python :: 3.8",
            "Programming Language :: Python :: 3.9",
            "Programming Language :: Python :: 3.10",
            "Programming Language :: Python :: Implementation :: CPython",
            "Programming Language :: Python :: Implementation :: PyPy",
        ],
        package_data={"returnlabels": ["icons/*", "configs/*.yml", "translate/*"]},
        entry_points={
            "console_scripts": [
                "returnlabels=returnlabels.main:main",
            ],
        },
    )


if __name__ == "__main__":
    main()
